package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.response.ResponseData;
import com.ruoyi.business.service.BasePayment;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.HttpResponse;
import com.ruoyi.common.utils.HttpUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

@Service("yxePayService")
public class YxePayServiceImpl  extends BasePayment {

    @Override
    public JSONObject createPayOrder(TOrderInfo orderInfo) throws Exception {
        JSONObject jsonObject = new JSONObject();
        TRecipientInfo userRecipientInfo = userRecipientService.selectTRecipientInfoById(orderInfo.getRecipientId());
        HashMap<String, String> headerMap = new HashMap();
        HashMap<String, String> dataMap = new HashMap();
        String cookieStr = userRecipientInfo.getCookie();
        JSONObject cookieObj = JSONObject.parseObject(cookieStr);
        String qrcode = cookieObj.getString("qrcode");
        headerMap.put("Referer","https://longpay.xddshare.com/ysepay/h5/?tx=wz&qrcode="+qrcode);
        headerMap.put("User-Agent","Nebula AlipayDefined(nt:WIFI,ws:360|0|3.0) AliApp(AP/10.0.18.062203) AlipayClient/10.0.18.062203 Language/zh-Hans useStatusBar/true");

//
//        String url = "https://api.umvtech.com/api/payment/getUserName";
//        dataMap.put("user_code",userRecipientInfo.getPhoneNo());
//        HttpResponse priceHttpResponse = HttpUtils.httpFormPost(url,headerMap,"",dataMap,"kiss查询用户请求","");
//        JSONObject resultObj = JSONObject.parseObject(priceHttpResponse.getDocument().body().text());
//        int resultCode = resultObj.getInteger("status");
//        String priceId = "";
//        if(resultCode==1){
//
//
//
//        }
        //https://qr.leshuazf.com/pay/qrc.do?qr=SYTD200387629

        logger.info("qrcode："+qrcode);
        String url ="https://longpay.xddshare.com/ysepay/checkout/api/get/merchant/name/"+qrcode;
        logger.info("获取银小二信息："+url);

        HttpResponse httpResponse = HttpUtils.httpFormGet(url,headerMap,"","银小二");
        logger.info("返回的信息:"+httpResponse.getDocument().text());
        JSONObject resultObj = JSONObject.parseObject(httpResponse.getDocument().text());
        if(resultObj.getIntValue("code")!=0){
            jsonObject.put("code","100");
            jsonObject.put("msg","获取连接失败");
            logger.info("========修改收款账号为不可用===============");
            userRecipientInfo.setChannelDisabled(1);
            userRecipientService.updateTRecipientInfo(userRecipientInfo);
            return jsonObject;
        }
        JSONObject dataObj = resultObj.getJSONObject("data");
        String codeId = dataObj.getString("codeId");
        String sellerId = dataObj.getString("sellerId");
        cookieObj.put("codeId",codeId);
        cookieObj.put("sellerId",sellerId);

        logger.info("挂码省份：" + userRecipientInfo.getProvince());
        logger.info("挂码城市：" + userRecipientInfo.getCity());
        JSONObject ipObject = commonService.getPoxyIp(orderInfo);
        if(!ipObject.getString("code").equalsIgnoreCase("200")){
            jsonObject.put("code","100");
            jsonObject.put("msg",ipObject.getString("msg"));
            return jsonObject;
        }
        String ip = ipObject.getString("ip");
        String port = ipObject.getString("port");
        cookieObj.put("latitude",ipObject.getString("latitude"));
        cookieObj.put("longitude",ipObject.getString("longitude"));
        String proxyIp =ip + ":" + port;
        cookieObj.put("proxyIp",proxyIp);
        orderInfo.setQrcodeTxt(cookieObj.toJSONString());
        orderInfoService.updateTOrderInfo(orderInfo);
        jsonObject.put("code","200");
        return jsonObject;
    }


    public JSONObject createWxPayOrder(TOrderInfo orderInfo) throws ServiceException, UnsupportedEncodingException {
        JSONObject jsonObject = new JSONObject();
        TRecipientInfo userRecipientInfo = userRecipientService.selectTRecipientInfoById(orderInfo.getRecipientId());
        HashMap<String, String> headerMap = new HashMap();
        HashMap<String, String> dataMap = new HashMap();
        String cookieStr = userRecipientInfo.getCookie();
        JSONObject cookieObj = JSONObject.parseObject(cookieStr);
        headerMap.put("User-Agent","Nebula AlipayDefined(nt:WIFI,ws:360|0|3.0) AliApp(AP/10.0.18.062203) AlipayClient/10.0.18.062203 Language/zh-Hans useStatusBar/true");

//
//        String url = "https://api.umvtech.com/api/payment/getUserName";
//        dataMap.put("user_code",userRecipientInfo.getPhoneNo());
//        HttpResponse priceHttpResponse = HttpUtils.httpFormPost(url,headerMap,"",dataMap,"kiss查询用户请求","");
//        JSONObject resultObj = JSONObject.parseObject(priceHttpResponse.getDocument().body().text());
//        int resultCode = resultObj.getInteger("status");
//        String priceId = "";
//        if(resultCode==1){
//
//
//
//        }
        //https://qr.leshuazf.com/pay/qrc.do?qr=SYTD200387629

        String url ="https://shop.shouyinbei.net/app/wxapp.php?i=1&v=undefined&m=we7_wmall&c=entry&do=mobile&ctrl=wmall&ac=store&op=paybill&ta=getNewStoreInfo&from=wxapp&state=we7sid-1f5947214065a723ec571946828f80ac&code_id=95069&type=2&is_pay=1&sid=undefined";
        logger.info("获取银小二信息："+url);

        HttpResponse httpResponse = HttpUtils.httpFormGet(url,headerMap,"","银小二");
        logger.info("返回的信息:"+httpResponse.getDocument().text());
        JSONObject resultObj = JSONObject.parseObject(httpResponse.getDocument().text());
        if(resultObj.getIntValue("code")!=0){
            jsonObject.put("code","100");
            jsonObject.put("msg","获取连接失败");
            logger.info("========修改收款账号为不可用===============");
            userRecipientInfo.setChannelDisabled(1);
            userRecipientService.updateTRecipientInfo(userRecipientInfo);
            return jsonObject;
        }
        JSONObject dataObj = resultObj.getJSONObject("data");
        String codeId = dataObj.getString("codeId");
        String sellerId = dataObj.getString("sellerId");
        cookieObj.put("codeId",codeId);
        cookieObj.put("sellerId",sellerId);
        logger.info("挂码省份：" + userRecipientInfo.getProvince());
        logger.info("挂码城市：" + userRecipientInfo.getCity());
        JSONObject ipObject = commonService.getPoxyIp(orderInfo);
        String ip = ipObject.getString("ip");
        String port = ipObject.getString("port");
        cookieObj.put("latitude",ipObject.getString("latitude"));
        cookieObj.put("longitude",ipObject.getString("longitude"));
        String proxyIp =ip + ":" + port;
        cookieObj.put("proxyIp",proxyIp);
        orderInfo.setQrcodeTxt(cookieObj.toJSONString());
        orderInfoService.updateTOrderInfo(orderInfo);
        jsonObject.put("code","200");
        return jsonObject;
    }

    @Override
    public ResponseData smartReissueOrder(TOrderInfo orderInfo) throws Exception {
        return new ResponseData();
    }
}
